﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace OpenSunTracker.RestClient.Extensions
{
    public static class XmlTools
    {     
        public static string ToXmlString<T>(this T obh)
        {
            TextWriter stringWriter = new StringWriter();
            var xmlSerializer = new System.Xml.Serialization.XmlSerializer(obh.GetType());            
            xmlSerializer.Serialize(stringWriter, obh);
            return stringWriter.ToString();
        }
      

        public static IEnumerable<T> ToTypesFromXmlString<T>(this string xmlString)
        {
            var textReader = new StringReader(xmlString);
            var datas = new List<T>();
            var xmlSerializer = new System.Xml.Serialization.XmlSerializer(datas.GetType());
            return xmlSerializer.Deserialize(textReader) as IList<T>;

        }


        public static T ToTypeFromXmlString<T>(this string xmlString) where T : class
        {
            Type type = typeof(T);
            var xmlSerializer = new System.Xml.Serialization.XmlSerializer(type);
            var textReader = new StringReader(xmlString);
            return xmlSerializer.Deserialize(textReader) as T;

        }
    }
}
